package com.qymerge.manage.base.controller;

import com.google.common.reflect.TypeToken;
import com.qymerge.manage.base.dto.export.CardExportDTO;
import com.qymerge.manage.base.ro.MetaCompanyRO;
import com.qymerge.manage.base.service.IMetaCompanyService;
import com.qymerge.manage.core.bean.*;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.controller.BaseController;
import com.qymerge.manage.core.exception.ApiException;
import com.qymerge.manage.core.utils.*;
import com.qymerge.manage.system.ro.SysUserRO;
import com.qymerge.manage.system.service.ISystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * <p>Title: IMetaCompanyManageController</p>
 * <p>Description: 法人公司</p>
 *
 * @author Xiao
 * @date 2025/4/29 17:18
 * @version 1.0.0
 */
@Controller
@RequestMapping(value = "base/company")
public class IMetaCompanyManageController extends BaseController {

    @Autowired
    private IMetaCompanyService metaCompanyService;
    @Autowired
    private ISystemService iSystemService;

    /**
     * 列表
     *
     * @param parameters
     * @param callback
     */
    @RequestMapping("list")
    public void list(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<MetaCompanyRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaCompanyRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaCompanyRO ro = paramValue.getData();

        // 非系统管理员，登录用户根据绑定的关联公司查询数据
        SysUserRO sysUser = iSystemService.getSysUser(common.getUid());
        if (ObjectUtils.isEmpty(sysUser)) {
            RequestCheckUtils.checkNotFalse( false,  "请确认登录后，在请求数据");
        }
        if(common.getUid() != -1) {
            ro.setId(sysUser.getCompany_id());
        }

        DataTableRO data = metaCompanyService.list(ro, new PageRequest(common.getPage(), common.getSize()));
        PrintCodeUtils.printSuccess(response, data.build(), callback);
    }

    /**
     * 详情
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("detail")
    public void detail(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {

        ParamValue<MetaCompanyRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaCompanyRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaCompanyRO ro = paramValue.getData();

        RequestCheckUtils.checkNotEmpty(ro.getId(), "ID必须");
        MetaCompanyRO resultRO = metaCompanyService.detail(ro.getId());

        PrintCodeUtils.printSuccess(response, resultRO, callback);
    }


}
